//
//  RBSettingVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/15.
//

#import "RBSettingVC.h"
#import <UIColor+YYAdd.h>
#import "RBThemeConfig.h"
#import <UIControl+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBDefine.h"
#import <Masonry.h>
#import "UIButton+RBAdd.h"
#import "RBProfileVC.h"
#import "RBSecurityVC.h"
#import "RBAddresssListVC.h"
#import "RBFeedbackVC.h"
#import "RBAboutVC.h"
#import "RBAppDelegateHelper.h"
#import "RBRegisterInfoResponse.h"
#import "RBRegisterStateVC.h"
#import "UIViewController+RBAdd.h"

@interface RBSettingVC ()

@property(nonatomic, strong)UIButton *exitBtn;

@end

@implementation RBSettingVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"设置";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithImage:[[UIImage imageNamed:@"settingQuestion"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(questionBtnTapped)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:nil forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    __weak typeof(self) weakSelf = self;
    [self.view view_addView:[self cellWithTitle:@"个人资料" des:@"头像、昵称、注册信息等" block:^{
        RBProfileVC *vc = RBProfileVC.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:[self cellWithTitle:@"收货地址" des:nil block:^{
        RBAddresssListVC *vc = RBAddresssListVC.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:[self cellWithTitle:@"账号安全" des:@"修改账号密码、支付密码信息" block:^{
        RBSecurityVC *vc = RBSecurityVC.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
//    [self.view view_addView:[self cellWithTitle:@"注册信息" des:nil block:^{
//        [weakSelf registerInfoCellTapped];
//    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];

    [self.view view_addView:[self cellWithTitle:@"问题反馈" des:nil block:^{
        RBFeedbackVC *vc = RBFeedbackVC.new;
        [self.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:[self cellWithTitle:@"清除缓存" des:nil block:^{
        
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:[self cellWithTitle:@"关于我们" des:nil block:^{
        RBAboutVC *vc = RBAboutVC.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    self.exitBtn.hidden = NO;
}

#pragma mark - Network

#pragma mark - Event

- (void)questionBtnTapped
{
    
}

- (void)registerInfoCellTapped
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"han_user_reginfo" post:YES log:YES responseClass:RBRegisterInfoResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBRegisterInfoResponse *response) {
        RBRegisterStateVC *vc = RBRegisterStateVC.new;
        vc.loginResponse = ({
            RBLoginResponse *res = RBLoginResponse.new;
            res.data = RBLoginModel.new;
            res.data.user_id = [RBAccountManager getUserId];
            res.data.step = response.data.step;
            res;
        });
        [weak_self.navigationController pushViewController:vc animated:YES];
    } failBlock:^(RBRegisterInfoResponse *response) {
        [weak_self.view showTip:response.message completion:nil];
    } reloadBlock:nil];
}

- (void)exitBtnTapped
{
    __weak typeof(self) weakSelf = self;
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出登录" preferredStyle:UIAlertControllerStyleAlert];
    [vc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [vc addAction:[UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [RBAccountManager setUserId:nil];
        [RBAccountManager setToken:nil];
        RBAppDelegateHelper.keyWindow.rootViewController = RBAppDelegateHelper.tabbarVC;
    }]];
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)cellWithTitle: (NSString *)title des: (NSString *)des block: (void(^)(void))block
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleL = ({
        UILabel *view = UILabel.new;
        view.font = [UIFont systemFontOfSize:Zoom(14)];
        view.textColor = [RBThemeConfig textBlackColor];
        view.text = title;
        view;
    });
    [view view_addView:titleL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(25) bottom:Zoom(25) left:Zoom(18) right:CGFLOAT_MIN centerX:0];
    
    if (des.length)
    {
        UILabel *desL = ({
            UILabel *view = UILabel.new;
            view.font = [UIFont systemFontOfSize:Zoom(12)];
            view.textColor = [UIColor colorWithHexString:@"#ACACAC"];
            view.text = des;
            view;
        });
        [view view_addView:desL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(11) bottom:Zoom(25) left:Zoom(18) right:CGFLOAT_MIN centerX:0];
    }
    
    UIImageView *arrowImgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"mine_arrow_right"]];
    [view addSubview:arrowImgView];
    [arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(18));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(7));
        make.height.mas_equalTo(Zoom(13));
    }];

    UIButton *btn = UIButton.new;
    [view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [btn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        if (block)
        {
            block();
        }
    }];
    
    return view;
}

- (UIButton *)exitBtn
{
    UIButton *view = UIButton.new;
    [view rbAddTarget:self action:@selector(exitBtnTapped)];
    [view rbSetTitle:@"退出账号"];
    [view rbSetTitleColor:UIColor.whiteColor];
    [view rbSetFont:[UIFont systemFontOfSize:Zoom(15)]];
    [view rbSetCornerRadius:Zoom(22)];
    view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-MAX(Zoom(25), kRBSafeBottomHeight));
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(Zoom(339), Zoom(44)));
    }];
    return view;
}

@end
